<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

{% extends 'admin/base.html' %}
{% block head_css %}
  {{ super() }}
  <link href="{{ url_for('static', filename='bootstrap-theme.css') }}" rel="stylesheet">
  <link rel="icon" type="image/png" href="{{ url_for("static", filename="pin_30.png") }}">
  <link rel="stylesheet" type="text/css" href="{{ url_for("static", filename="main.css") }}">
{% endblock %}

{% block tail_js %}
{{ super() }}
<script src="{{ url_for('static', filename='jqClock.min.js') }}" type="text/javascript"></script>
<script>
    x = new Date()
    var UTCseconds = (x.getTime() + x.getTimezoneOffset()*60*1000);
    $("#clock").clock({
        "dateFormat":"Y-m-d ",
        "timeFormat":"H:i:s %UTC%",
        "timestamp":UTCseconds
    }).click(function(){
        alert('{{ hostname }}');
    });
    $('span').tooltip();
    $('time[title]').tooltip();
    var CSRF = {{ csrf_token() | tojson }};

    $.ajaxSetup({
      beforeSend: function(xhr, settings) {
        if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
          xhr.setRequestHeader("X-CSRFToken", CSRF);
        }
      }
    });

var el = document.createElement("span");

function escapeHtml(text) {
  el.textContent = text;
  return el.innerHTML;
}

function postAsForm(url, parameters) {
  var form = $("<form></form>");

  form.attr("method", "POST");
  form.attr("action", url);

  $.each(parameters || {}, function(key, value) {
    var field = $('<input></input>');

    field.attr("type", "hidden");
    field.attr("name", key);
    field.attr("value", value);

    form.append(field);
  });

  var field = $('<input></input>');

  field.attr("type", "hidden");
  field.attr("name", "csrf_token");
  field.attr("value", CSRF);

  form.append(field);

  // The form needs to be a part of the document in order for us to be able
  // to submit it.
  $(document.body).append(form);
  form.submit();
}

function convertSecsToHumanReadable(seconds) {
   var oriSeconds = seconds
   var floatingPart = oriSeconds- Math.floor(oriSeconds)

   seconds = Math.floor(seconds)

   var secondsPerHour = 60 * 60;
   var secondsPerMinute = 60;

   var hours = Math.floor(seconds / secondsPerHour);
   seconds = seconds - hours * secondsPerHour;

   var minutes = Math.floor(seconds / secondsPerMinute);
   seconds = seconds - minutes * secondsPerMinute;

   var readableFormat = ''
   if (hours > 0) {
     readableFormat += hours + "Hours ";
   }
   if (minutes > 0) {
     readableFormat += minutes + "Min ";
   }
   if (seconds + floatingPart > 0) {
     if (Math.floor(oriSeconds) === oriSeconds) {
       readableFormat += seconds + "Sec";
     } else {
       seconds += floatingPart
       readableFormat += seconds.toFixed(3) + "Sec";
     }
   }
   return readableFormat
}
</script>
{% endblock %}

{% block page_body %}
<div class="container">

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" style="background-color: {{ navbar_color }};">
    <div class="container">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#admin-navbar-collapse">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
          <a class="navbar-brand" rel="home" href="{{ url_for('admin.index') }}" style="cursor: pointer;">
              <img style="float: left; width:35px; margin-top: -7px;"
                   src="{{ url_for("static", filename="pin_100.png") }}"
                   {% if current_user.user is defined %}
                      title="{{ current_user.user.username }}"
                   {% else %}
                      title="{{ current_user.username }}"
                   {% endif %}>
              <span>Airflow</span>
          </a>
      </div>
      <!-- navbar content -->
      <div class="collapse navbar-collapse" id="admin-navbar-collapse">
        {% block main_menu %}
        <ul class="nav navbar-nav">
          {{ layout.menu() }}
        </ul>
        {% endblock %}

        {% block menu_links %}
        <ul class="nav navbar-right">
          {{ layout.menu_links() }}
        </ul>
        {% endblock %}
        <!-- clock and logout -->
        <ul class="nav navbar-nav navbar-right">
            <li><a id="clock"></a></li>
            {% if current_user.is_authenticated %}
              <li class="never_active"><a href="{{ url_for('airflow.logout') }}"><span data-toggle="tooltip" data-placement="left" title="Logout" class="glyphicon glyphicon-log-out"></span></a></li>
            {% endif %}
        </ul>
          <ul class="nav navbar-nav navbar-right">
              <li><a id="clock"></a></li>
          </ul>
      </div>
      </div>
</nav>
  {% block messages %}
  {% if scheduler_job is defined and (not scheduler_job or not scheduler_job.is_alive()) %}
    <div class="alert alert-warning">
      <p>The scheduler does not appear to be running.
      {% if scheduler_job %}
      Last heartbeat was received <time title="{{ scheduler_job.latest_heartbeat.isoformat() }}" datetime="{{ scheduler_job.latest_heartbeat.isoformat() }}">{{ macros.datetime_diff_for_humans(scheduler_job.latest_heartbeat) }}</time>.
      {% endif %}
      </p>
      <p>The DAGs list may not update, and new tasks will not be scheduled.</p>
    </div>
  {% endif %}
  {{ layout.messages() }}
  {% endblock %}

  {# store the jinja2 context for form_rules rendering logic #}
  {% set render_ctx = h.resolve_ctx() %}
  {% block body %}
    {{ content }}
  {% endblock %}

</div>
{% endblock %}

